/**
 * 时间戳转日期格式
 * @param timeStamp
 * @param type 类型
 * @returns
 */
export function timeStampTurnTime(timeStamp:any, type = 1) {
    if (timeStamp != undefined && timeStamp != "" && timeStamp > 0) {
        let date = new Date();
        date.setTime(timeStamp * 1000);
        let y = date.getFullYear();
        let m: string | number = date.getMonth() + 1;
        m = m < 10 ? "0" + m : m;
        var d: string | number = date.getDate();
        d = d < 10 ? "0" + d : d;
        var h: string | number = date.getHours();
        h = h < 10 ? "0" + h : h;
        var minute: string | number = date.getMinutes();
        var second: string | number = date.getSeconds();
        minute = minute < 10 ? "0" + minute : minute;
        second = second < 10 ? "0" + second : second;
        if (type == 1) {
            return y + "-" + m + "-" + d;
        } else if (type == 2) {
            return y + "年" + m + "月" + d + "日";
        } else if (type == 3) {
            return (
                y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second
            );
        } else {
            return (
                y +
                "年" +
                m +
                "月" +
                d +
                "日 " +
                h +
                "时" +
                minute +
                "分" +
                second +
                "秒"
            );
        }
    } else {
        return "";
    }
}

/**
 * 日期格式转时间戳
 * @param {Object} timeStamp
 */
export function timeTurnTimeStamp(value: string) {
    let f = value.split(" ", 2);
    let d = (f[0] ? f[0] : "").split("-", 3);
    let t = (f[1] ? f[1] : "").split(":", 3);
    return (
        new Date(
            parseInt(d[0], 10) || 0,
            (parseInt(d[1], 10) || 1) - 1,
            parseInt(d[2], 10) || 0,
            parseInt(t[0], 10) || 0,
            parseInt(t[1], 10) || 0,
            parseInt(t[2], 10) || 0
        ).getTime() / 1000
    );
}

/**
 * 获取当前毫秒数
 * @returns 毫秒时间戳
 */
export function currentMillisecond() {
    const date = new Date();
    return date.getTime();
}

/**
 * 获取当前的秒数
 * @returns 秒时间戳
 */
export function currentSeconds() {
    const date = new Date();
    return parseInt(date.getTime() / 1000 + "");
}

/**
 * 返回今日开始和结束的时间戳
 * @returns
 */
export function todayBetween() {
    let date = new Date();
    date.setHours(0);
    date.setMinutes(0);
    date.setSeconds(0);
    date.setMilliseconds(0);
    let t = date.getTime() / 1000;
    return [t, t + (86400 - 1)];
}

/**
 * 倒计时
 * @param seconds
 * @returns
 */
export function countDown(seconds: number) {
    let day: string | number = 0,
        hour: string | number = 0,
        minute: string | number = 0,
        second: string | number = 0;
    if (seconds > 0) {
        day = Math.floor(seconds / (60 * 60 * 24));
        hour = Math.floor(seconds / (60 * 60)) - day * 24;
        minute = Math.floor(seconds / 60) - day * 24 * 60 - hour * 60;
        second =
            Math.floor(seconds) -
            day * 24 * 60 * 60 -
            hour * 60 * 60 -
            minute * 60;
    }
    if (day < 10) {
        day = "0" + day;
    }
    if (hour < 10) {
        hour = "0" + hour;
    }
    if (minute < 10) {
        minute = "0" + minute;
    }
    if (second < 10) {
        second = "0" + second;
    }
    return {
        d: day,
        h: hour,
        i: minute,
        s: second,
    };
}
